package Java练习.字符串练习;

import java.util.Random;
import java.util.Scanner;

public class 打乱字符串 {
    public static void main(String[] args) {
        //思路,从左边开始向右边冒泡,但是替换的是右边的随机索引的值.
        //1.获取键盘输入的字符串
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入您要打乱的字符串:");
        String input=sc.next();
        //2.把字符串转换为char[]
        char[] ch=input.toCharArray();
        //3.对char[]进行打乱
        for (int i = 0; i < ch.length; i++) {
            //获取随机索引
            Random ra=new Random();
            int index =ra.nextInt(ch.length);
            //定义一个临时变量
            char tep=ch[i];
            ch[i]=ch[index];
            ch[index]=tep;
        }
        //把char[]字符数组变回变成String
        String str=new String(ch);
        System.out.println(str);


    }
}
